<?php

namespace App\Http\Controllers\Setting;

use App\Models\Opinion;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class OpinionController extends Controller
{
    /** 问题列表 */
    public function index(Request $request)
    {
        $type = $request->input('type');
        $status = $request->input('status');
        if($request->isMethod('post')){
            $model = Opinion::when(!empty($type),function($query)use($type){
                    $query->where('type',$type);
                })
                ->when(!empty($status),function($query)use($status){
                    $query->where('status',$status);
                })
                ->orderBy('updated_at','desc')
                ->paginate(config('base.limit'));
            return $this->data($model->items(),$model->total());
        }
        return view('setting.opinion.index',['type'=>$type,'status'=>$status]);
    }

    /** 处理 */
    public function check(Request $request)
    {
        $id = $request->get('id');
        $model = Opinion::find($id);
        if($request->isMethod('post')){
            $data = $request->all();

            $model->status = $data['status'];
            $model->save();

            return $this->success('操作成功!');
        }

        return view('setting.opinion.check',['model'=>$model]);
    }
}
